package ua.com.infinite.mobile;

import ua.com.infinite.mobile.utils.SharedPreferencesUtils;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.support.v4.app.NavUtils;

public class LauncherActivity extends Activity {

	public static int LOGIN = 11;
	public static int MAIN_ACTIVITY = 12;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		App.setContext(getApplicationContext());
		
		Intent intent;
		if (SharedPreferencesUtils.isLogin(this)) {
			intent = new Intent(LauncherActivity.this, MainActivity.class);
			startActivityForResult(intent,MAIN_ACTIVITY);
		} else {
			intent = new Intent(LauncherActivity.this, LoginActivity.class);
			startActivityForResult(intent,LOGIN);
		}
		
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data){
		super.onActivityResult(requestCode, resultCode, data);
		Intent intent;
		if(requestCode == LOGIN){
			if(resultCode == RESULT_OK & SharedPreferencesUtils.isLogin(this)){
				intent = new Intent(LauncherActivity.this, MainActivity.class);
				startActivityForResult(intent,MAIN_ACTIVITY);
			}else{
				finish();
			}
		}else if(requestCode == MAIN_ACTIVITY){
			if(resultCode == RESULT_OK & !SharedPreferencesUtils.isLogin(this)){
				intent = new Intent(LauncherActivity.this, LoginActivity.class);
				startActivityForResult(intent,LOGIN);
			}else{
				finish();
			}
		}
	}

}
